package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.shopping.dao.MemberDao;
import com.yc.shopping.util.ShoppingUtil;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Dialog_FindMember extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_mid;
	private Table table;
	MemberDao dao =new MemberDao();
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Dialog_FindMember(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		shell.setSize(450, 300);
		shell.setText(getText());
		
		Label label = new Label(shell, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setBounds(20, 17, 76, 24);
		label.setText("会员编号:");
		
		text_mid = new Text(shell, SWT.BORDER);
		text_mid.setBounds(102, 18, 121, 23);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(20, 56, 394, 166);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("会员编号");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("会员名称");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("生日");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(86);
		tableColumn_3.setText("积分");
		
		Button button = new Button(shell, SWT.NONE);
		ShoppingUtil.centerWindows(shell);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {	
				showMember();
				text_mid.setText("");
			}
		});
		button.setBounds(306, 16, 80, 27);
		button.setText("查询");
		showMember();
	}
	
	private void showMember()  {
		String mid = text_mid.getText().trim();
		List<Map<String,Object>> list =null;
		
		try {
			if("".equals(mid)) {
				list=dao.selectMemberMessage();
			}else {
				list=dao.selectOneMember(mid);
			}
			if(list ==null || list.isEmpty()) {
				ShoppingUtil.showMessage(shell, "提示", "该会员不存在");
				return;
			}
			//清空表格中原有的数据
			table.removeAll();
			TableItem tableItem =null;
			for (Map<String, Object> map : list) {
				tableItem = new TableItem(table,SWT.NONE);
				tableItem.setText(new String[] {
						ShoppingUtil.objToString(map.get("MID")),
						ShoppingUtil.objToString(map.get("NAME")),
						ShoppingUtil.objToString(map.get("BIRTHDAY")),
						ShoppingUtil.objToString(map.get("SCORE")),
				});
			}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}				
	}
	
	
}
